/**
 * 
 */
$.fn.selege=function(option){
	var settings=$.extend({
		width:'auto',
		height:'auto',
		editable:false,
		sly:false,
		onSelect:function(){},
		onOpen:function(){}
	},option);
	$(this).each(function(){
		var obj=$(this);
		var str='';
		obj.find('option').each(function(){
			var s_obj=$(this);
			str+='<li val="'+s_obj.val()+'">'+s_obj.html()+'</li>';
		});
		obj.hide().after('<div class="selege"><div class="value"><input type="text"/></div><div class="options"><ul>'+str+'</ul></div></div>');
		var s_obj=obj.next('.selege');
		var t_obj=s_obj.find('input');
		var o_obj=s_obj.find('.options');
		var u_obj=s_obj.find('ul');
		//初始化大小
		if (settings.width=='auto'){
			s_obj.width(obj.width());
			t_width=obj.width()-35;
			o_obj.width(obj.width()-2);
		}else if (settings.width>0){
			s_obj.width(settings.width);
			t_width=settings.width-35;
			o_obj.width(settings.width-2);
		}
		t_obj.width(t_width);
		s_obj.find('input').val(obj.find('option:selected').html());
		if (settings.sly&&typeof $.fn.sly=='function'&&settings.height!='auto'){
			//sly 滚动条
			o_obj.css({'overflow':'hidden','height':settings.height+'px'}).append('<div class="scrollbar"><div class="handle"></div></div>');
			s_obj.find('.scrollbar').height(settings.height-5);
			s_obj.find('.handle').height(settings.height*30/100);
		}else if (!settings.sly&&settings.height!='auto'){
			//原生滚动条
			o_obj.css({'overflow-y':'auto','height':settings.height+'px'});
		}

		var showOpt=function(){
			s_obj.css('z-index','101').find('.options').show();
			if (settings.sly){
				s_obj.find('.scrollbar').show();
				o_obj.sly({
					scrollBar: s_obj.find('.scrollbar'),
					dragHandle: 1,
					dynamicHandle: 1,
					dragContent: 1,
					speed: 50,
					startAt: 0,
					scrollBy: 20,
					elasticBounds: 1
				},true);
			}
			settings.onOpen();
		};
		var hideOpt=function(){
			o_obj.hide();
			if (settings.sly){
				s_obj.find('.scrollbar').hide();
			}
		};
		var clickClose=function(){hideOpt();$(document).unbind('click',clickClose);};
		s_obj.find('.value').click(function(){
			if (o_obj.is(":visible")){
				hideOpt();
			}else{
				$('.selege').css('z-index','100').find('.options').hide();
				showOpt();
				//$(document).bind('click',clickClose);
			}
		});
		var checkVal=function(){
			var same=false;
			obj.find('option').each(function(){
				if ($(this).val()==t_obj.val()){
					same=true;
					obj.find('option').removeAttr('selected');
					$(this).attr('selected','selected');
					return false;
				}
			});
			if (same===false){
				obj.find('option').removeAttr('selected');
				obj.append('<option selected="selected" auto="true" value="'+t_obj.val()+'">'+t_obj.val()+'</option>');
			}else{
				
			}
			obj.change();
		};
		if (settings.editable)
			t_obj.blur(function(){
				checkVal();
			}).click(function(){
				if (typeof t_obj.attr('sel')!='undefined'&&t_obj.attr('sel')=='true')
					t_obj.removeAttr('sel');
				else
					t_obj.select().attr('sel','true');
			});
		else{
			t_obj.attr('readonly','readonly');
			s_obj.find('.value').css('cursor','pointer');
		}
		/*s_obj.find('.value,input').click(function(){
			if (o_obj.is(":hidden"))
				showOpt();
		});*/
		s_obj.find('ul li').click(function(){
			hideOpt();
			t_obj.val($(this).html());
			if (typeof $(this).attr('val')!='undefined')
				obj.val($(this).attr('val'));
			else
				obj.val($(this).html());
			obj.change();
			$(document).unbind('click',clickClose);
			settings.onSelect();
		}).hover(function(){$(this).addClass('sel');},function(){$(this).removeClass('sel');});
	});
};